/*********************************************************/
/*                                                       */
/* Warleague                                             */
/*                                                       */
/* playstate.cpp                                         */
/*                                                       */
/*********************************************************/

/*********************************************************/
/* includes                                              */
/*********************************************************/
#include <SFML/Graphics.hpp>
#include "gamestate.h"
#include "engine.h"
#include "playstate.h"
#include "menustate.h"
#include "math.h"
#include "iostream"
#include "stdio.h"

CPlayState CPlayState::m_PlayState;

void CPlayState::Init()
{
    m_TextureLT.LoadFromFile("images/tank16x16.png");

    m_TextureChoosen.LoadFromFile("images/chosen22x22.png");

    m_TextureLicht.LoadFromFile("images/lichtwirbe32x32.png");
    m_SpriteLicht.SetTexture(m_TextureLicht);
    m_SpriteLicht.SetOrigin(m_TextureLicht.GetWidth()/2, m_TextureLicht.GetHeight()/2);
    m_SpriteLicht.SetPosition(200, 200);

    m_TextureLichtGreen.LoadFromFile("images/lichtwirbel32x32green.png");
    m_SpriteLichtGreen.SetTexture(m_TextureLichtGreen);
    m_SpriteLichtGreen.SetOrigin(m_TextureLichtGreen.GetWidth()/2, m_TextureLichtGreen.GetHeight()/2);
    m_SpriteLichtGreen.SetPosition(400, 200);

    listCTank.clear();

    for (int i = 0; i < 5; i++)
    {
        listCTank.push_back(new CTank(&m_TextureLT, &m_TextureChoosen, sf::Vector2f(10,10), 20.f));
        listCTank.back()->SetPosition (20, (i+1)*100);
        listCTank.back()->SetSpeed(40);
    }

    m_font.LoadFromFile("font/FreeSans.ttf");
    m_TextFps.SetString("Test");
    m_TextFps.SetPosition(0, 0);
    m_TextFps.SetCharacterSize(14);
    m_TextFps.SetStyle(sf::Text::Bold);
    m_TextFps.SetColor(sf::Color(244, 167, 0));
    m_ClockFps.Reset();
    count = 0;
    m_shift_pressed = false;
}

void CPlayState::Cleanup()
{
    while(!listCTank.empty())
    {
        listCTank.back()->~CTank();
        listCTank.pop_back();
    }
}

void CPlayState::Pause()
{
}

void CPlayState::Resume()
{
}

void CPlayState::HandleEvents(CGameEngine* game)
{
    sf::Event Event;

    while (game->App->PollEvent(Event))
    {
        // Close window : exit
        if (Event.Type == sf::Event::Closed)
        {
            game->Quit();
        }


        // A key has been pressed
        if (Event.Type == sf::Event::KeyPressed)
        {
            if (Event.Key.Code == sf::Keyboard::Escape)
            {
                game->ChangeState(CMenuState::Instance());
                //game->PopState();
            }

            if (Event.Key.Code == sf::Keyboard::LShift)
            {
                m_shift_pressed = true;
            }
        }

        if (Event.Type == sf::Event::KeyReleased)
        {
            if(Event.Key.Code == sf::Keyboard::LShift)
            {
                m_shift_pressed = false;
            }
        }

        if (Event.Type == sf::Event::MouseButtonPressed)
        {
            if (Event.MouseButton.Button == sf::Mouse::Left)
            {
                for (iCTank = listCTank.begin (); iCTank != listCTank.end (); iCTank++)
                {
                    if ((*iCTank)->CollisionCirclePoint(Event.MouseButton.X, Event.MouseButton.Y))
                    {
                        (*iCTank)->m_choosen = true;
                    }
                    else
                    {
                        (*iCTank)->m_choosen = false;
                    }
                }
            }

            if (Event.MouseButton.Button == sf::Mouse::Right)
            {
                for (iCTank = listCTank.begin (); iCTank != listCTank.end (); iCTank++)
                {
                    if ((*iCTank)->m_choosen)
                    {
                        if (m_shift_pressed)
                        {
                            (*iCTank)->m_waypoints.push_back(sf::Vector2f(float(Event.MouseButton.X), float(Event.MouseButton.Y)));
                        }
                        else
                        {
                            (*iCTank)->m_waypoints.clear();
                            (*iCTank)->m_waypoints.push_back(sf::Vector2f(float(Event.MouseButton.X), float(Event.MouseButton.Y)));
                        }
                    }
                }
            }
        }
    }
}

void CPlayState::Update(CGameEngine* game)
{
    for (iCTank = listCTank.begin (); iCTank != listCTank.end (); iCTank++)
    {
        (*iCTank)->Update();
    }

    m_SpriteLicht.Rotate(10*game->App->GetFrameTime());
    m_SpriteLichtGreen.Rotate(25*game->App->GetFrameTime());

    char buffer[100];
    sprintf(buffer, "Fps: %d", int(count/m_ClockFps.GetElapsedTime()*1000));
    m_TextFps.SetString(buffer);
}

void CPlayState::Draw(CGameEngine* game)
{
    game->App->Clear (sf::Color (0, 0, 0));

    game->App->Draw(m_TextFps);

    for (iCTank = listCTank.begin (); iCTank != listCTank.end (); iCTank++)
    {
        (*iCTank)->DrawSprites(game);
    }

    game->App->Draw(m_SpriteLicht);
    game->App->Draw(m_SpriteLichtGreen);

    game->App->Display ();

    count++;
}

void CPlayState::FadeOut (CGameEngine* game)
{
    for (int i = 0; i < 255; i++)
    {
        Draw (game);
    }
}


